/*
  Copyright (c), 2001-2022, Shenshu Tech. Co., Ltd.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <signal.h>

#include "sample_comm.h"

static vdec_display_cfg g_vdec_display_cfg_cvbs = {
    .pic_size = PIC_576P,
    .intf_sync = OT_VO_OUT_PAL,
    .intf_type = OT_VO_INTF_CVBS,
};
// static vdec_display_cfg g_vdec_display_cfg = {
//     .pic_size = PIC_576P,
//     .intf_sync = OT_VO_OUT_USER,
//     .intf_type = OT_VO_INTF_RGB_24BIT,
// };

// static ot_vo_sync_info  _576p_sync_info = {
//     .syncm = TD_TRUE,
//     .iop = TD_TRUE,
//     .intfb = 0,
//     .hpw = 24, /* 24 pixel */
//     .hbb = 112, /* 80 pixel */
//     .hact = 720, /* 1200 pixel */
//     .hfb = 22, /* 60 pixel */
//     .vpw = 5, /* 2 line */
//     .vbb = 44, /* 10 line */
//     .vact = 576, /* 1920 line */
//     .vfb = 5, /* 15 line */
// 	.idv = 0,
// 	.ihs =1,
// 	.ivs = 1,
// };

// ot_vo_user_sync_info _user_sync={
//    .user_sync_attr = {
// 	   .clk_src = OT_VO_CLK_SRC_PLL_FOUT4,
// 	   .vo_pll = { 
// 		   .fb_div = 72,
// 		   .frac = 0,
// 		   .ref_div = 1,
// 		   .post_div1 = 4, 
// 		   .post_div2 = 2,
// 	   },
//    },
//    .pre_div = 1,
//    .dev_div = 1, 
//    .clk_reverse_en = TD_TRUE,
// };


static ot_size g_disp_size = {.width = 720,.height = 576};
static td_s32 g_sample_exit = 0;

static td_s32 sample_start_vo(sample_vo_cfg *vo_config)
{
    td_s32 ret;
    vo_config->vo_dev            = SAMPLE_VO_DEV_DHD1;
    vo_config->vo_intf_type      = g_vdec_display_cfg_cvbs.intf_type;
    vo_config->intf_sync         = g_vdec_display_cfg_cvbs.intf_sync;
    vo_config->pic_size          = g_vdec_display_cfg_cvbs.pic_size;
    vo_config->bg_color          = COLOR_RGB_BLUE;
    vo_config->dis_buf_len       = 3; /* 3:buf length */
    vo_config->dst_dynamic_range = OT_DYNAMIC_RANGE_SDR8;
    vo_config->vo_mode           = VO_MODE_1MUX;
    vo_config->pix_format        = OT_PIXEL_FORMAT_YVU_SEMIPLANAR_420;
    vo_config->disp_rect.x       = 0;
    vo_config->disp_rect.y       = 0;
    vo_config->disp_rect.width   = g_disp_size.width;
    vo_config->disp_rect.height  = g_disp_size.height;
    vo_config->image_size.width  = g_disp_size.width;
    vo_config->image_size.height = g_disp_size.height;
    vo_config->vo_part_mode      = OT_VO_PARTITION_MODE_SINGLE;
    vo_config->compress_mode     = OT_COMPRESS_MODE_NONE;
	vo_config->dev_frame_rate = 30;

	ret = sample_comm_vo_start_vo(vo_config);
    if (ret != TD_SUCCESS) {
        sample_print("start VO fail for %#x!\n", ret);
        sample_comm_vo_stop_vo(vo_config);
        return ret;
    }
	// vo_config->vo_dev            = SAMPLE_VO_DEV_DHD0;
	// vo_config->vo_intf_type      = g_vdec_display_cfg.intf_type;
    // vo_config->intf_sync         = g_vdec_display_cfg.intf_sync;
    // vo_config->pic_size          = g_vdec_display_cfg.pic_size;
	// (td_void)memcpy_s(&vo_config->user_sync, sizeof(ot_vo_user_sync_info), &_user_sync, sizeof(ot_vo_user_sync_info));
	// (td_void)memcpy_s(&vo_config->sync_info, sizeof(ot_vo_sync_info), &_576p_sync_info, sizeof(ot_vo_sync_info));
	
    // ret = sample_comm_vo_start_vo(vo_config);
    // if (ret != TD_SUCCESS) {
    //     sample_print("start VO fail for %#x!\n", ret);
    //     sample_comm_vo_stop_vo(vo_config);
    //     return ret;
    // }

    return ret;
}

td_s32 vo_cvbs_sys_init()
{
    td_s32 ret;
    sample_vo_cfg vo_config;

    ret = sample_start_vo(&vo_config);
    if (ret != TD_SUCCESS) {
        sample_print("error sample_start_vo %x\n",ret);
    }

    return ret;
}